<template>
	<view class="uni-column">

		<view class="content" id="content" :style="{height:style.contentViewHeight+'px'}">
			<scroll-view id="scrollview" scroll-y="true" :style="{height:style.contentViewHeight+'px'}" :scroll-with-animation="true"
			    :scroll-top="scrollTop">
				<message-show v-for="(message,index) in messages" :key="index" v-bind:message="message" :id="index"></message-show>
				<view id="bottom"></view>
			</scroll-view>
		</view>

		<view class="foot">
			<chat-input @send-message="getInputMessage"></chat-input>
		</view>

	</view>
</template>

<script>
	
	import chatInput from '../../components/chatinput.vue';
	import messageShow from '../../components/messageshow.vue';
	import main from './main.js'

	export default {

		data() {
			return {
				style: {
					pageHeight: 0,
					contentViewHeight: 0,
					footViewHeight: 90,
					mitemHeight: 0,
				},
				scrollTop: 0,
				messages: [{
					user: 'home',
					type: 'head', //input,content 
					content: '你好!'
				},{
					user: 'home',
					type: 'head', //input,content 
					content: '你好!'
				},{
					user: 'home',
					type: 'head', //input,content 
					content: '你好!'
				},{
					user: 'home',
					type: 'head', //input,content 
					content: '你好!'
				},{
					user: 'home',
					type: 'head', //input,content 
					content: '你好!'
				},{
					user: 'home',
					type: 'head', //input,content 
					content: '你好!'
				},{
					user: 'home',
					type: 'head', //input,content 
					content: '你好!'
				},{
					user: 'home',
					type: 'head', //input,content 
					content: '你好!'
				}]
			}
		},

		components: {
			chatInput,
			messageShow
		},

		created: function () {
			main.refreshContentHeigth(this);
		},

		methods: {

			input_focus: function () {
				main.refreshContentHeigth(this);
				main.srollToBottom(this);
			},

			getInputMessage: function (message) { //获取子组件的输入数据
				// console.log(message);
				this.addMessage('customer', message.content, false);
				this.toRobot(message.content);
			},

			addMessage: function (user, content, hasSub, subcontent) {
				var that = this;
				that.messages.push({
					user: user,
					content: content,
					hasSub: hasSub,
					subcontent: subcontent
				});
			},

			scrollToBottom: function () {
				var that = this;
				main.srollToBottom(this);
			},
			toRobot: function (info) {

				// this.addMessage('home', info, false);
				var apiUrl = 'http://www.tuling123.com/openapi/api';
				uni.request({
					url: apiUrl,
					data: {
						"key": 'acfbca724ea1b5db96d2eef88ce677dc',
						"info": info,
						"userid": 'uni-test'
					},
					
					success: (res) => {
						
						this.addMessage('home', res.data.text, false);
						
						this.scrollToBottom();
					},
					
					fail: (err) => {
						uni.showModal({
							content: err.errMsg,
							showCancel: false
						})
					}
				});
			}
		}
	}
</script>

<style>
	.uni-column {
		display: flex;
		flex-direction: column;
	}

	.content {

		display: flex;
		flex: 1;
		margin-bottom: 100px;
	}

	.foot {
		position: fixed;
		width: 100%;
		height: 90px;
		min-height: 90px;
		left: 0px;
		bottom: 0px;
		overflow: hidden;
	}
</style>
